<?php

//就近 www.jiujin.me
if (!defined('IN_IA')) {
	die('Access Denied');
}
global $_W, $_GPC;
$openid = m('user')->getOpenid();
//$set = m('common')->getSysset(array('shop', 'trade'));
$member = m('member')->getInfo($openid);
//是否扫码进来的进行判断
if(isset($_GPC['record'])){
	$record = $_GPC['record'];
	$id = isset($_GET['id'])?$_GET['id']:0;
	//判断是否关注
	//if($member['uid']){
	if(!empty($_W['fans']['follow'])){
	
	}else{
		$forward = 'http://w.url.cn/s/Arhp5c7';
		header('Location: ' . $forward);
		die();
		exit();
	}
	//先判断扫没扫过
	$qr = pdo_fetch("SELECT * FROM ".tablename('qrcode_record')." WHERE uniacid = :uniacid AND record = :record AND iid = :iid LIMIT 1", array(':uniacid' => $_W['uniacid'], ':record' => $record, 'iid' => $id));
	//123
	$saoma = pdo_fetch("SELECT goods,jifen FROM ".tablename('ewei_shop_saoma')." WHERE uniacid = :uniacid AND id = :id LIMIT 1", array(':uniacid' => $_W['uniacid'], 'id' => $id));
	if($saoma['goods']){
		$a = strripos($saoma['goods'],'couponid') + 9;
		$couponid = substr($saoma['goods'],$a);
	}
	if($saoma['jifen']){
		$b = strripos($saoma['goods'],'couponid') + 9;
		$couponid = substr($saoma['jifen'],$b);
	}
	
	//再判断有没有领过
	$ling = pdo_fetch("SELECT * FROM ".tablename('activity_coupon_record')." WHERE uniacid = :uniacid AND uid = :uid AND couponid = :couponid LIMIT 1", array(':uniacid' => $_W['uniacid'], ':uid' => $member['uid'], 'couponid' => $couponid));
	if($qr && $ling){
		//已经扫过了,提示
		include $this->template('saoma/used');
		die();
		
	}else{
		if($id){
			$insert = array(
				'uniacid' => $_W['uniacid'],
				'openid' => $openid,
				'iid' => $id,
				'record' => $record,
				'createtime' => TIMESTAMP,
			);
			pdo_insert('qrcode_record', $insert);
		}else{
			//禁止进入
			print_r(2);
		}
	}
	
	if($id == 0){
		$saoma = pdo_fetch('select * from ' . tablename('ewei_shop_saoma') . ' where  uniacid=:uniacid and status=1 order by id desc', array(':uniacid' => $_W['uniacid']));
	}else{
		$saoma = pdo_fetch('select * from ' . tablename('ewei_shop_saoma') . ' where  uniacid=:uniacid and status=1 and id=:id order by id desc', array(':uniacid' => $_W['uniacid'],':id'=>$id));
	}
	include $this->template('saoma/list');
}else{
	//判断是否关注
	if($member['uid']){
	
	}else{
	
	}
	$id = isset($_GET['id'])?$_GET['id']:0;
	if($id == 0){
		$saoma = pdo_fetch('select * from ' . tablename('ewei_shop_saoma') . ' where  uniacid=:uniacid and status=1 order by id desc', array(':uniacid' => $_W['uniacid']));
	}else{
		$saoma = pdo_fetch('select * from ' . tablename('ewei_shop_saoma') . ' where  uniacid=:uniacid and status=1 and id=:id order by id desc', array(':uniacid' => $_W['uniacid'],':id'=>$id));
	}
	include $this->template('saoma/list');
}
